﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.Common;
using ZX.DBModule;
using ZX.DAL.DBExtend;
using System.Data;

namespace ZX.Web.UI.Controls.WebExtend
{
    public class SumFields
    {
        private string entityName;

        public SumFields(string entityName)
        {
            this.entityName = entityName;
        }

        public DataTable GetSumResult(string condition)
        {
            DataTable setTotalFields = new DataTable();
            String sumStr = "";

            DbSql db = new DbSql();
            setTotalFields = db.ExecuteQuery("select * from Set_TotalFields where EntityName = '" + this.entityName + "' order by Sort");

            if (setTotalFields.Rows.Count != 0)
            {
                for (int i = 0; i < setTotalFields.Rows.Count; i++ )
                {
                    sumStr = sumStr + (sumStr == "" ? "" : ", ") + "sum(" + setTotalFields.Rows[i]["FieldName"] + ") as [" + setTotalFields.Rows[i]["FieldDes"] + "]";
                }
            }

            return db.ExecuteQuery("select " + sumStr + " from " + this.entityName + " where 1=1 " + (condition == "" ? "" : "and ") + condition);

        }
    }
}
